package com.cy.dda.service.goodsdiligencing.impl;

import com.cy.dda.comm.utils.CommonUtils;
import com.cy.dda.comm.utils.SpringContextUtils;
import com.cy.dda.dao.mapper.goodsdiligencing.GoodsDiligencingMaintainReportMapper;
import com.cy.dda.dao.mapper.goodsdiligencing.GoodsDiligencingMapper;
import com.cy.dda.dao.mapper.goodsdiligencing.GoodsDiligencingReturnVisitReportMapper;
import com.cy.dda.dao.mapper.goodsdiligencing.GoodsDiligencingVerifyTruthReportMapper;
import com.cy.dda.model.PageBean;
import com.cy.dda.model.Result;
import com.cy.dda.model.ao.goodsdiligencing.GoodsDiligencingReportAuditAO;
import com.cy.dda.model.domain.goodsdiligencing.GoodsDiligencing;
import com.cy.dda.model.domain.goodsdiligencing.GoodsDiligencingMaintainReport;
import com.cy.dda.model.domain.goodsdiligencing.GoodsDiligencingReturnVisitReport;
import com.cy.dda.model.domain.goodsdiligencing.GoodsDiligencingVerifyTruthReport;
import com.cy.dda.model.dto.mall.PushLoanOrderInfoToDdaDTO;
import com.cy.dda.model.enums.diligencing.DiligencingStatus;
import com.cy.dda.model.enums.goodsdiligencing.GoodsDiligencingConclusion;
import com.cy.dda.model.enums.goodsdiligencing.GoodsDiligencingStatus;
import com.cy.dda.model.enums.goodsdiligencing.GoodsDiligencingType;
import com.cy.dda.model.query.goodsdiligencing.GoodsDiligencingAdminListQuery;
import com.cy.dda.model.query.goodsdiligencing.GoodsDiligencingListQuery;
import com.cy.dda.model.vo.goodsdiligencing.*;
import com.cy.dda.service.BaseService;
import com.cy.dda.service.goodsdiligencing.GoodsDiligencingService;
import com.cy.dda.service.goodsdiligencing.MaintainReportService;
import com.cy.dda.service.goodsdiligencing.ReturnVisitReportService;
import com.cy.dda.service.goodsdiligencing.VerifyTruthReportService;
import com.cy.dda.service.ppj.PpjService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description 家具尽调 service 接口实现类
 * @Author tanyilin
 * @Date 2019/8/23 16:38
 */
@Slf4j
@Service
public class GoodsDiligencingServiceImpl extends BaseService implements GoodsDiligencingService {

    @Autowired
    private GoodsDiligencingMapper                  goodsDiligencingMapper;
    @Autowired
    private GoodsDiligencingVerifyTruthReportMapper verifyTruthReportMapper;
    @Autowired
    private GoodsDiligencingReturnVisitReportMapper returnVisitReportMapper;
    @Autowired
    private GoodsDiligencingMaintainReportMapper maintainReportMapper;
    @Autowired
    private PpjService                              ppjService;
    @Autowired
    private VerifyTruthReportService verifyTruthReportService;
    @Autowired
    private ReturnVisitReportService returnVisitReportService;
    @Autowired
    private MaintainReportService maintainReportService;

    @Override
    @Transactional
    public void addGoodsDiligencing(PushLoanOrderInfoToDdaDTO pushLoanOrderInfoToDdaDTO) {

        GoodsDiligencing dbGoodsDiligencing = new GoodsDiligencing();
        dbGoodsDiligencing.setMOrderId(pushLoanOrderInfoToDdaDTO.getOrderId());
        dbGoodsDiligencing.setMLoanId(pushLoanOrderInfoToDdaDTO.getLoanId());
        int count = goodsDiligencingMapper.selectCount(dbGoodsDiligencing);
        if(count > 0) {
            log.info("该家具订单已存在，[orderId = {}，loanId = {}]", pushLoanOrderInfoToDdaDTO.getOrderId(), pushLoanOrderInfoToDdaDTO.getLoanId());
            return;
        }

        Date time = new Date();

        GoodsDiligencing goodsDiligencing = new GoodsDiligencing();
        BeanUtils.copyProperties(pushLoanOrderInfoToDdaDTO, goodsDiligencing);
        goodsDiligencing.setMOrderId(pushLoanOrderInfoToDdaDTO.getOrderId());
        goodsDiligencing.setMLoanId(pushLoanOrderInfoToDdaDTO.getLoanId());
        goodsDiligencing.setMCustomerId(pushLoanOrderInfoToDdaDTO.getCustomerId());
        goodsDiligencing.setDiligencingType(GoodsDiligencingType.VERIFY_TRUTH_INVESTIGATE);
        goodsDiligencing.setDiligencingStatus(DiligencingStatus.DISTRIBUTE_WAIT);
        goodsDiligencing.setStatus(GoodsDiligencingStatus.UN_INVESTIGATE);
        goodsDiligencing.setConclusion(GoodsDiligencingConclusion.NONE);
        goodsDiligencing.setCreateTime(time);
        goodsDiligencing.setUpdateTime(time);
        goodsDiligencingMapper.insertSelective(goodsDiligencing);

    }

    @Override
    public PageBean<GoodsDiligencingListVO> getGoodsDiligencingList(GoodsDiligencingListQuery query) {

        int total = goodsDiligencingMapper.selectGoodsDiligencingListCount(query);
        List<GoodsDiligencingListVO> list = new ArrayList<>();
        if(total > 0) {
            query.startPage();
            list = goodsDiligencingMapper.selectGoodsDiligencingList(query);
            list.stream().forEach(gd -> {
                gd.setDiligencingStatusText(CommonUtils.isEmpty(gd.getDiligencingStatus()) ? null : gd.getDiligencingStatus().getText());
                gd.setSex(CommonUtils.getSexByIdNo(gd.getIdNo()));
                gd.setIdNo(null);
                if(query.getFlag() == 2) {
                    gd.setSummary("尽调".concat(CommonUtils.isEmpty(gd.getConclusion()) ? "" : gd.getConclusion().getText())
                            .concat("，")
                            .concat(gd.getConclusionDesc()));
                    gd.setDiligencingStatus(DiligencingStatus.COMPLETED);
                    gd.setDiligencingStatusText(DiligencingStatus.COMPLETED.getText());
                    gd.setReportReturnReason(null);
                }
            });
        }

        return new PageBean<>(list, total);
    }

    @Override
    public Result getGoodsDiligencingDetail(Long goodsDiligencingId) {

        GoodsDiligencing dbGoodsDiligencing = goodsDiligencingMapper.selectByPrimaryKey(goodsDiligencingId);
        state(dbGoodsDiligencing != null, "家具尽调详情数据异常");

        Result result = ppjService.getGoodsDiligencingDetail(dbGoodsDiligencing.getMLoanId(), dbGoodsDiligencing.getMOrderId());

        return result;
    }

    @Override
    public GoodsDiligencingStatusVO getGoodsDiligencingStatus(Long goodsDiligencingId) {

        GoodsDiligencing dbGoodsDiligencing = goodsDiligencingMapper.selectByPrimaryKey(goodsDiligencingId);
        state(dbGoodsDiligencing != null, "家具尽调详情数据异常");

        GoodsDiligencingVerifyTruthReport verifyTruthReport = new GoodsDiligencingVerifyTruthReport();
        verifyTruthReport.setGoodsDiligencingId(goodsDiligencingId);
        verifyTruthReport = verifyTruthReportMapper.selectOne(verifyTruthReport);

        GoodsDiligencingReturnVisitReport returnVisitReport = new GoodsDiligencingReturnVisitReport();
        returnVisitReport.setGoodsDiligencingId(goodsDiligencingId);
        returnVisitReport = returnVisitReportMapper.selectOne(returnVisitReport);

        GoodsDiligencingMaintainReport maintainReport = new GoodsDiligencingMaintainReport();
        maintainReport.setGoodsDiligencingId(goodsDiligencingId);
        maintainReport = maintainReportMapper.selectOne(maintainReport);

        GoodsDiligencingStatusVO statusVO = new GoodsDiligencingStatusVO();
        statusVO.setStatus(dbGoodsDiligencing.getStatus());
        statusVO.setDiligencingType(dbGoodsDiligencing.getDiligencingType());
        statusVO.setDiligencingStatus(dbGoodsDiligencing.getDiligencingStatus());
        if(verifyTruthReport != null) {
            statusVO.setVerifyTruthTime(verifyTruthReport.getUpdateTime());
        }
        if(returnVisitReport != null) {
            statusVO.setReturnVisitTime(returnVisitReport.getUpdateTime());
        }
        if(maintainReport != null) {
            statusVO.setMaintainTime(maintainReport.getUpdateTime());
        }

        return statusVO;
    }

    @Override
    public PageBean<GoodsDiligencingAdminListVO> getGoodsDiligencingAdminList(GoodsDiligencingAdminListQuery query) {

        int total = goodsDiligencingMapper.selectGoodsDiligencingAdminListCount(query);
        List<GoodsDiligencingAdminListVO> list = new ArrayList<>();
        if(total > 0) {
            query.startPage();
            list = goodsDiligencingMapper.selectGoodsDiligencingAdminList(query);
            list.stream().forEach(gd -> {
                gd.setDiligencingTypeText(gd.getDiligencingType().getText());
                gd.setStatusText(gd.getStatus().getText());
                gd.setConclusionText(gd.getConclusion().getText());
                gd.setAlreadyLoanDays(CommonUtils.diffDays(gd.getLoanTime(), new Date()));
            });
        }
        return new PageBean<>(list, total);
    }

    @Override
    @Transactional
    public void appointDistributeGoodsDiligencing(Long goodsDiligencingId, GoodsDiligencingType diligencingType, Long userId) {

        Date time = new Date();

        GoodsDiligencing update = new GoodsDiligencing();
        update.setId(goodsDiligencingId);
        update.setUserId(userId);
        update.setDistributeTime(time);
        update.setUpdateTime(time);
        update.setDiligencingStatus(DiligencingStatus.PROCESSING);
        switch (diligencingType) {
            case VERIFY_TRUTH_INVESTIGATE:
                //验真分配
                update.setDiligencingType(GoodsDiligencingType.VERIFY_TRUTH_INVESTIGATE);
                update.setStatus(GoodsDiligencingStatus.VERIFY_TRUTH_ING);
                break;
            case RETURN_VISIT_INVESTIGATE:
                //回访分配
                update.setDiligencingType(GoodsDiligencingType.RETURN_VISIT_INVESTIGATE);
                update.setStatus(GoodsDiligencingStatus.RETURN_VISIT_ING);
                break;
            case MAINTAIN_INVESTIGATE:
                //维护分配
                update.setDiligencingType(GoodsDiligencingType.MAINTAIN_INVESTIGATE);
                update.setStatus(GoodsDiligencingStatus.MAINTAIN_ING);
                break;
            default:
                break;
        }
        goodsDiligencingMapper.updateByPrimaryKeySelective(update);

    }

    @Override
    @Transactional
    public void doAuditGoodsDiligencingReport(GoodsDiligencingReportAuditAO auditAO, Long managerId) {

        GoodsDiligencingType diligencingType = auditAO.getDiligencingType();

        GoodsDiligencing dbGoodsDiligencing = goodsDiligencingMapper.selectByPrimaryKey(auditAO.getGoodsDiligencingId());
        state(diligencingType.equals(dbGoodsDiligencing.getDiligencingType()), "调查报告类型异常");

        switch (diligencingType) {
            case VERIFY_TRUTH_INVESTIGATE:
                //审核验真报告
                SpringContextUtils.getBean(VerifyTruthReportService.class).doAuditVerifyTruthReport(auditAO, managerId);
                break;
            case RETURN_VISIT_INVESTIGATE:
                //审核回访报告
                SpringContextUtils.getBean(ReturnVisitReportService.class).doAuditReturnVisitReport(auditAO, managerId);
                break;
            case MAINTAIN_INVESTIGATE:
                //审核维护报告
                SpringContextUtils.getBean(MaintainReportService.class).doAuditMaintainReport(auditAO, managerId);
                break;
            default:
                break;
        }

    }

    @Override
    public GoodsDiligencingAdminDetailVO getGoodsDiligencingAdminDetail(Long goodsDiligencingId) {

        GoodsDiligencing dbGoodsDiligencing = goodsDiligencingMapper.selectByPrimaryKey(goodsDiligencingId);
        state(dbGoodsDiligencing != null, "尽调资料数据异常");

        GoodsDiligencingAdminDetailVO detailVO = new GoodsDiligencingAdminDetailVO();
        detailVO.setGoodsDiligencingId(dbGoodsDiligencing.getId());
        detailVO.setDiligencingType(dbGoodsDiligencing.getDiligencingType());

        //验真调查报告
        VerifyTruthReportAdminDetailVO verifyTruthReport = verifyTruthReportService.getVerifyTruthReportAdminDetail(goodsDiligencingId);
        detailVO.setVerifyTruthReport(verifyTruthReport);

        //回访调查报告
        ReturnVisitReportAdminDetailVO returnVisitReport = returnVisitReportService.getReturnVisitReportAdminDetail(goodsDiligencingId);
        detailVO.setReturnVisitReport(returnVisitReport);

        //维护调查报告
        MaintainReportAdminDetailVO maintainReport = maintainReportService.getMaintainReportAdminDetail(goodsDiligencingId);
        detailVO.setMaintainReport(maintainReport);

        return detailVO;
    }

    @Override
    public Result getGoodsDiligencingApplyInfo(Long goodsDiligencingId) {

        GoodsDiligencing dbGoodsDiligencing = goodsDiligencingMapper.selectByPrimaryKey(goodsDiligencingId);
        state(dbGoodsDiligencing != null, "家具尽调详情数据异常");

        Result result = ppjService.getGoodsDiligencingApplyInfo(dbGoodsDiligencing.getMOrderId(), dbGoodsDiligencing.getMCustomerId());

        return result;
    }


}
